
import { mapState } from 'vuex'
// 播放器



export const playListMixin = {
  computed: {
    ...mapState(['playList'])
  },
  mounted() {
    this.handlePlayList(this.playList)
  },
  activated() {
    this.handlePlayList(this.playList)
  },

}
export const playMixin = {
  computed: {
    ...mapState(['playList'])
  },
  mounted() {
    this.handlePlayList()
  },
  activated() {
    this.handlePlayList()
  },
}
// 收藏歌曲
export const operatorMixin = {

  computed: {
    ...mapState(['loveMusic']),
  }, methods: {
    getFavoriteIcon(song) {
      const i = this.isFavorite(song);
      return i > -1 ? 'icon-favorite' : 'icon-not-favorite'
    },
    isFavorite(song) {
      const i = this.loveMusic.findIndex(v => v.mid === song.mid);
      return i
    },
    toggleFavorite(song) {
      const i = this.isFavorite(song);

      if (i > -1) {
        this.$store.commit('removeLove', i)
      } else {
        this.$store.commit('unshiftLove', song)
      }
      return i > -1 ? 'icon-not-favorite' : 'icon-favorite'
    },
  }
}

export const playerMixin = {
  data() {
    return {
      favoriteIcon: ''
    }
  },
  computed: {
    ...mapState(['loveMusic', 'currentSong']),
  },
  mounted() {
    this.getFavoriteIcon()
  },
  methods: {
    getFavoriteIcon() {
      const i = this.isFavorite(this.currentSong);
      this.favoriteIcon = i > -1 ? 'icon-favorite' : 'icon-not-favorite'
    },
    local(arr, name) {
      let len = arr.length
      if (len) {
        arr = arr.filter(v => v.mid !== this.currentSong.mid);
      }
      if (len > 100) {
        arr.pop()
      }
      arr.unshift(this.currentSong)

      localStorage.setItem(name, JSON.stringify(arr))
    },
    isFavorite(song) {
      const i = this.loveMusic.findIndex(v => v.mid === song.mid);
      return i
    },
    toggleFavorite() {
      const i = this.isFavorite(this.currentSong);
      this.favoriteIcon = i > -1 ? 'icon-not-favorite' : 'icon-favorite'
      if (i > -1) {
        this.$store.commit('removeLove', i)
      } else {
        this.$store.commit('unshiftLove', this.currentSong)
      }
    },
    resetCurrentIndex(list) {
      const i = list.findIndex(v => v.mid === this.currentSong.mid);
      this.$store.commit('setCurrentIndex', i)
    }
  },
}
// 收藏歌手
export const singerMixin = {
  computed: {
    ...mapState({
      loveSinger: 'loveSinger'
    })
  },
  methods: {
    toggleFavorite(singer) {
      const index = this.isFavorite(singer)
      if (index > -1) {
        // 已关注歌手
        this.loveSinger.splice(index, 1)
      } else { // 这首歌没有收藏
        this.loveSinger.unshift(singer)
      }
      localStorage.setItem("loveSinger", JSON.stringify(this.loveSinger))
    },
    getFavoriteText(singer) {
      const index = this.isFavorite(singer)
      if (index > -1) {
        return '已关注'
      } else {
        return '关注'
      }
    },
    // 判断是否收藏歌曲
    isFavorite(singer) {
      const index = this.loveSinger.findIndex((item) => {
        return item.singer_id === singer.singer_id
      })
      // 有收藏歌曲,索引
      return index
    }
  }
}
export const listMixin = {
  computed: {
    ...mapState({
      value: 'value',
      loveSongList: 'loveSongList',
      loveAlbum: 'loveAlbum',
      loveRank: 'loveRank',
      loveSinger: 'loveSinger',
      songList: 'songList',
      album: 'album',
      rank: 'rank',
      singer: 'singer'
    })
  },
  methods: {
    toggleFavorite() {
      const index = this.isFavorite()
      let loveList, item, loveLocal
      let value = this.value
      if (value == "歌单") {
        loveList = this.loveSongList
        item = this.songList
        loveLocal = "loveSongList"
      }
      if (value == "歌手") {
        loveList = this.loveSinger
        item = this.singer
        loveLocal = "loveSinger"
      }
      if (value == "排行") {
        loveList = this.loveRank
        item = this.rank
        loveLocal = "loveRank"
      }
      if (value == "专辑") {
        loveList = this.loveAlbum
        item = this.album
        loveLocal = "loveAlbum"
      }
      if (index > -1) {
        // 已有这首收藏歌单，不再收藏
        loveList.splice(index, 1)
      } else { // 歌单没有收藏
        loveList.unshift(item)
      }
      localStorage.setItem(loveLocal, JSON.stringify(loveList))
    },
    getFavoriteIcon() {
      const index = this.isFavorite()
      if (index > -1) {
        return 'icon-favorite'
      } else {
        return 'icon-not-favorite'
      }
    },
    // 判断是否收藏歌曲
    isFavorite() {
      let index
      if (this.value == "歌单") {
        index = this.loveSongList.findIndex((item) => {
          return item.content_id === this.songList.content_id
        })
        return index
      }
      if (this.value == "排行") {
        index = this.loveRank.findIndex((item) => {
          return item.topId === this.rank.topId
        })
        return index
      }
      if (this.value == "专辑") {
        index = this.loveAlbum.findIndex((item) => {
          return item.albumMID === this.album.albumMID
        })
        return index
      }
      if (this.value == "歌手") {
        index = this.loveSinger.findIndex((item) => {
          return item.singer_id === this.singer.singer_id
        })
        return index
      }
    }
  }
}